package com.example.jedis.client.command.string;

import com.example.jedis.client.command.constant.RedisCommandConstant;
import com.example.jedis.client.protocol.constant.RedisProtocolConstant;
import com.example.jedis.client.protocol.request.RequestProtocol;
import com.example.jedis.client.protocol.response.ResponseProtocol;
import com.example.jedis.client.utils.RedisProtocolUtils;

public class RedisStringCommandImpl implements RedisStringCommand {

    RequestProtocol requestProtocol = null;
    ResponseProtocol responseProtocol = null;


    public RequestProtocol getRequestProtocol() {
        return requestProtocol;
    }

    public void setRequestProtocol(RequestProtocol requestProtocol) {
        this.requestProtocol = requestProtocol;
    }

    public ResponseProtocol getResponseProtocol() {
        return responseProtocol;
    }

    public void setResponseProtocol(ResponseProtocol responseProtocol) {
        this.responseProtocol = responseProtocol;
    }


    public String  set(String key, String value){
        requestProtocol.write(RedisCommandConstant.COMMAND_SET_BYTES, RedisProtocolUtils.toBytes(key),RedisProtocolUtils.toBytes(value));
        return (String)responseProtocol.read();
    }

    @Override
    public String setNX(String key, String value) {
        requestProtocol.write(RedisCommandConstant.COMMAND_SET_BYTES, RedisProtocolUtils.toBytes(key),RedisProtocolUtils.toBytes(value),RedisCommandConstant.COMMAND_NX_BYTES);
        return (String)responseProtocol.read();
    }

    @Override
    public String setXX(String key, String value) {
        requestProtocol.write(RedisCommandConstant.COMMAND_SET_BYTES, RedisProtocolUtils.toBytes(key),RedisProtocolUtils.toBytes(value),RedisCommandConstant.COMMAND_XX_BYTES);
        return (String)responseProtocol.read();
    }

    @Override
    public String set(String key, String value, int expireSeconds) {
        requestProtocol.write(RedisCommandConstant.COMMAND_SET_BYTES, RedisProtocolUtils.toBytes(key),RedisProtocolUtils.toBytes(value),RedisCommandConstant.COMMAND_EX_BYTES,RedisProtocolUtils.toBytes(expireSeconds));
        return (String)responseProtocol.read();
    }

    @Override
    public String setNX(String key, String value, int expireSeconds) {
        requestProtocol.write(RedisCommandConstant.COMMAND_SET_BYTES, RedisProtocolUtils.toBytes(key),RedisProtocolUtils.toBytes(value),RedisCommandConstant.COMMAND_EX_BYTES,RedisProtocolUtils.toBytes(expireSeconds),RedisCommandConstant.COMMAND_NX_BYTES);
        return (String)responseProtocol.read();
    }

    @Override
    public String setXX(String key, String value, int expireSeconds) {
        requestProtocol.write(RedisCommandConstant.COMMAND_SET_BYTES, RedisProtocolUtils.toBytes(key),RedisProtocolUtils.toBytes(value),RedisCommandConstant.COMMAND_EX_BYTES,RedisProtocolUtils.toBytes(expireSeconds),RedisCommandConstant.COMMAND_XX_BYTES);
        return (String)responseProtocol.read();
    }

    public String get(String key){
        requestProtocol.write(RedisCommandConstant.COMMAND_GET_BYTES,RedisProtocolUtils.toBytes(key));
        return (String)responseProtocol.read();
    }
}
